package br.com.cefetrn.apoena.dominio.home;

// Generated 01/01/2008 17:35:51 by Hibernate Tools 3.2.0.b11

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import br.com.cefetrn.apoena.dominio.entity.AreasAtuacaoProfissional;
import br.com.cefetrn.apoena.dominio.entity.AreasDeAtuacao;
import br.com.cefetrn.apoena.dominio.entity.Profissional;
import br.com.cefetrn.apoena.dominio.entity.QualificacaoProfissional;
import br.com.cefetrn.apoena.util.HibernateUtil;

/**
 * Home object for domain model class Profissional.
 * 
 * @see br.com.cefetrn.apoena.dominio.entity.Profissional
 * @author Gilmar P.S.L.
 */
public class Home4Profissional extends AxHomeGeneric<Profissional, Integer> {

	@Override
	public Profissional persist(Profissional instance) {
		
		Profissional profissional = null;
		
		if(instance != null){
			
			Home4Login hl = new Home4Login();
			hl.setSession(getSession());
			hl.persist(instance.getLogin());
			
			Home4Endereco he = new Home4Endereco();
			he.setSession(getSession());
			he.persist(instance.getEndereco());
			
			profissional = super.persist(instance);
			
			if(instance.getAreasAtuacaoProfissionals() != null && !instance.getAreasAtuacaoProfissionals().isEmpty()){
				
				Home4AreasAtuacaoProfissional hap = new Home4AreasAtuacaoProfissional();
				hap.setSession(getSession());
				
				for(AreasAtuacaoProfissional area : instance.getAreasAtuacaoProfissionals()){
					hap.persist(area);
				}
			}
			
		}
		
		return profissional;
	}
	
	
	
	@Override
	public Profissional attachDirty(Profissional instance) {
		
		Profissional p = super.attachDirty(instance);
		
		if(instance.getAreasAtuacaoProfissionals() != null && !instance.getAreasAtuacaoProfissionals().isEmpty()){
			
			Home4AreasAtuacaoProfissional hap = new Home4AreasAtuacaoProfissional();
			hap.setSession(getSession());
			
			for(AreasAtuacaoProfissional area : instance.getAreasAtuacaoProfissionals()){
				hap.attachDirty(area);
			}
		}
		
		return p;
	}



	public List<Profissional> findProfissionais(
			QualificacaoProfissional qualificacaoProfissional,
			AreasDeAtuacao areaAtuacao, String municipio) {
		String sql;
		StringBuilder s = new StringBuilder("Select p from Profissional p");
		StringBuilder sb = new StringBuilder(" where ");


		 if (qualificacaoProfissional != null &&
		 StringUtils.isNotBlank(qualificacaoProfissional.getNome())){
		   s.append(",QualificacaoProfissional q");
		   sb.append("q.nome like '%" +qualificacaoProfissional.getNome()+ "%' and q.profissional = p.id ");
		 }

		 if (StringUtils.isNotBlank(municipio) && qualificacaoProfissional != null && StringUtils.isNotBlank(qualificacaoProfissional.getNome())){
			 s.append(",Endereco e"); 
			 sb.append("and e.id = p.endereco and e.cidade like '%"+ municipio+"%' ");
			 
		 }else if(StringUtils.isNotBlank(municipio)){
			 s.append(",Endereco e");
			 sb.append(" e.id = p.endereco and e.cidade like '%"+ municipio+"%' ");
		 }
		 if(areaAtuacao != null && ( StringUtils.isNotBlank(municipio) || (qualificacaoProfissional != null))){
			 s.append(",AreasAtuacaoProfissional aap");
			 sb.append("and aap.profissional = p.id and aap.id =" + areaAtuacao.getId() ); 
		 } else if(areaAtuacao != null && ( (StringUtils.isNotBlank(municipio)) == false && (qualificacaoProfissional == null)) ){
			 s.append(",AreasAtuacaoProfissional aap");
			 sb.append(" aap.profissional = p.id and aap.id =" + areaAtuacao.getId());
		 }
		 
		 if(areaAtuacao == null || StringUtils.isNotBlank(municipio) || (qualificacaoProfissional == null))  {
			 sql = s.toString() + sb.toString();
			 Query query = getSession().createQuery(sql);
			 
			 System.out.println(query.list());
			 
			 //System.out.println(((Profissional)query.list().get(0)).getNome());
			 System.out.println(sql);
			 return query.list();
		 }
		 

		return null;

	}



	public static void main(String[] args) {

		QualificacaoProfissional q = new QualificacaoProfissional();
		q.setNome("gilmar");
		AreasDeAtuacao a = new AreasDeAtuacao();
		a.setId(1);
		Home4Profissional h4p = new Home4Profissional();
		h4p.setSession(HibernateUtil.currentSession());
		
		h4p.findProfissionais(null,a, null);
	}

}
